package com.gao.modules.login;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gao.base.Result;
import com.gao.constants.LoginConstants;
import com.gao.global.GlobalVariables;
import com.gao.modules.user.pojo.SysUser;
import com.gao.modules.user.service.SysUserService;
import com.gao.modules.user.vo.SysUserVo;
import com.gao.shiro.jwt.util.JwtUtil;
import com.gao.utils.CodeUtils;
import com.gao.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author DarkRepulser
 * @date 2021/11/9 11:40
 * @desc 登陆页
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取图形验证码
     */

    @GetMapping("/getCode")
    public Result getVerifyCode(){
//        uuid
        String uuid = IdUtil.simpleUUID();
        Map<String, String> map = new HashMap<>();
        //把key扔redis里
        try {
            String code = CodeUtils.getRandomVerifyCode(LoginConstants.LENGTH);
            redisTemplate.opsForValue().setIfAbsent(uuid, code,60, TimeUnit.SECONDS);
            String imgVerifyCode = CodeUtils.getImgVerifyCode(code);
            map.put("code",imgVerifyCode);
            map.put("uuid",uuid);
            return Result.success(map);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("生成验证码失败");
        }
    }

    @PostMapping("/doLogin")
    public Result doLogin(@RequestBody LoginParamsDto loginParamsDto){
        String code = (String) redisTemplate.opsForValue().get(loginParamsDto.getUuid());
        SysUser logingUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", loginParamsDto.getUserName()));
        if(logingUser == null){
            return Result.error("该用户不存在，请检查工号后重新输入！");
        } else if (code.equals(loginParamsDto.getVerifyCode())
            &&  logingUser.getPassword().equals(loginParamsDto.getPassWord())){
            SysUserVo sysUserVo = sysUserService.getUserDetailForLogin(logingUser);
            String token = JwtUtil.createToken(logingUser.getUserName(), logingUser.getPassword());
            redisUtil.setCommons((GlobalVariables.PREFIX_TOKEN + token), token, GlobalVariables.TOKEN_CACHE);
            sysUserVo.setToken(token);
            return Result.success(sysUserVo);
        }else if (!code.equals(loginParamsDto.getVerifyCode())){
            return Result.error("验证码不正确，请检查后重新输入！");
        }else if (!logingUser.getPassword().equals(loginParamsDto.getPassWord())){
            return Result.error("密码有误，请重新输入！");
        }else {
            return Result.error("未知错误，请联系管理员");
        }
    }

    @PostMapping("/checkName")
    public Result chackName(@RequestParam String loginName){
        int count = sysUserService.count(new QueryWrapper<SysUser>().eq("user_name", loginName));
        if(count == 1){
            return Result.success();
        } else {
            return Result.error();
        }
    }



}
